package com.liuxingyu.login.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.liuxingyu.login.data.User;
import com.liuxingyu.login.service.IUserService;

//声明为控制层类
@Controller
//声明Rest服务的类主路径
@RequestMapping("/login/user")
public class LoginController {
	@Autowired
	private IUserService userService;

	@RequestMapping("/loginin")
	public ModelAndView login(@RequestParam(value = "username", required = false) String userName,
			@RequestParam(value = "password", required = false) String password) {

		ModelAndView mv = new ModelAndView();
		Map<String, String> map = new HashMap<String, String>();
		if (userName != null && !"".equals(userName) && !"".equals(password) && password != null) {
			User user = userService.getByUserName(userName);
			if (user == null) {// 没有该用户
				map.put("msg", "没有该用户");
				mv.setViewName("demo/faillure");
			} else if (!password.equals(user.getPassword())) {// 用户密码不正确
				map.put("msg", "用户密码不正确");
				mv.setViewName("demo/faillure");
			} else {
				map.put("userName", userName);
				mv.setViewName("demo/main");
			}
		} else {
			map.put("msg", "用户名和密码不能为空");
			mv.setViewName("demo/faillure");
		}
		mv.addObject("data", map);
		return mv;

	}

	@RequestMapping("/loginPage")
	public ModelAndView loginPage() {

		ModelAndView mv = new ModelAndView();
		mv.setViewName("demo/login");
		return mv;
	}

	@RequestMapping(value = "/testResBody", method = RequestMethod.GET)
	@ResponseBody
	public Object testResBody() {
		Map map = new HashMap();
		map.put("name", "liuxingyu");
		map.put("sex", "nan");

		return map;
	}

}
